package cn.com.jsepc.rep.report;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Hashtable;
import java.util.List;
import java.util.Vector;

import javax.servlet.ServletException;

import cn.com.jsepc.rep.comm.ConstUtil;

import com.jarp.db.DAO;
import java.util.logging.Level;
import java.util.logging.Logger;

public class KeyCalc {
	private static String dqKey="REPVAL";//当期
	private static String ljKey="SUMVAL";//累计
	private static String tbKey="YOYRATION";//同比
	private static String hbKey="MONRATION";//环比
	private static String ljtbKey="TYOYRATION";//累计同比
	private static String ljhbKey="TMONRATION";//累计环比
	private static String sqKey="PREVAL";//上期
	private static String sqljKey="TPREVAL";//上期累计
	private static String [] extKeys={"YOYRATION","MONRATION","TYOYRATION","TMONRATION","PREVAL","TPREVAL","COLVAL1","COLVAL2","COLVAL3","COLVAL4","COLVAL5"};
	private static String [] keyKeys={"REPVAL","SUMVAL","YOYRATION","MONRATION","TYOYRATION","TMONRATION","PREVAL","TPREVAL"};
	
	public boolean CalcValues(String factTableName,String timeFieldName,String [] dimFields,Vector result){
		DAO dao=new DAO();
		ArrayList<String> sqls=new ArrayList();
		for(int i=0;i<result.size();i++){
			Hashtable curRecord=(Hashtable)result.get(i);
			String curTime="";
			if(curRecord.containsKey(timeFieldName)){
				curTime=curRecord.get(timeFieldName).toString();
			}else{
				//没有时间字段
				continue;
			}
			String curdqval=curRecord.get(dqKey).toString();
			String curdqljval=curRecord.get(ljKey).toString();
			String [] tbhbtimes=GetTimes(curTime,factTableName);
			String tbsql="select REPVAL,SUMVAL from "+factTableName+" where 1=1 ";
			String hbsql="select REPVAL,SUMVAL from "+factTableName+" where 1=1 ";
			String updateWhereStr="";
			String selectWhereStr="";
			for(int j=0;j<dimFields.length;j++){
				if(!dimFields[j].equals(timeFieldName)){
					selectWhereStr+=" and "+dimFields[j]+"="+curRecord.get(dimFields[j]).toString();
				}
				updateWhereStr+=" and "+dimFields[j]+"="+curRecord.get(dimFields[j]).toString();
			}
			//同比值查询语句
			tbsql+=selectWhereStr+" and "+timeFieldName+"="+tbhbtimes[0];
			hbsql+=selectWhereStr+" and "+timeFieldName+"="+tbhbtimes[1];
			try {
				//取同比值
				Vector tbV=dao.getDataSet(ConstUtil.DBNAME, tbsql);
				//取环比值
				Vector hbV=dao.getDataSet(ConstUtil.DBNAME, hbsql);
				//定义各更新值
				String tbval="",hbval="",ljtbval="",ljhbval="",sqval="",sqljval="";
				if(tbV!=null){
					Hashtable hst=(Hashtable)tbV.get(0);
					//同期值
					String tbstr=hst.get(dqKey).toString();
					if(!tbstr.equals("")){
						float tbf=Float.parseFloat(tbstr);
						if(!curdqval.equals("")){
							float curf=Float.parseFloat(curdqval);
							if(tbf==0.0){
								tbval="0.0";
							}else{
								//同比
								tbval=String.valueOf(curf/tbf*100);
							}
							
						}
					}
					//同期累计值
					String tbljstr=hst.get(ljKey).toString();
					if(!tbljstr.equals("")){
						float tbf=Float.parseFloat(tbljstr);
						if(!curdqljval.equals("")){
							float curf=Float.parseFloat(curdqljval);
							if(tbf==0.0){
								ljtbval="0.0";
							}else{
								ljtbval=String.valueOf(curf/tbf*100);
							}

						}
					}
				}
				if(hbV!=null){
					Hashtable hst=(Hashtable)hbV.get(0);
					//上期值
					String hbstr=hst.get(dqKey).toString();
					if(!hbstr.equals("")){
						//上期
						sqval=hbstr;
						float hbf=Float.parseFloat(hbstr);
						if(!curdqval.equals("")){
							float curf=Float.parseFloat(curdqval);
							if(hbf==0.0){
								hbval="0.0";
							}else{
								//环比
								hbval=String.valueOf(curf/hbf*100);
							}
							
						}
					}
					//上期期累计值
					String hbljstr=hst.get(ljKey).toString();
					if(!hbljstr.equals("")){
						//上期累计
						sqljval=hbljstr;
						float hbf=Float.parseFloat(hbljstr);
						if(!curdqljval.equals("")){
							float curf=Float.parseFloat(curdqljval);
							if(hbf==0.0){
								ljhbval="0.0";
							}else{
								ljhbval=String.valueOf(curf/hbf*100);
							}
							
						}
					}
				}
				String updatesql="update "+factTableName+" set ";
				String fields="";
				if(!tbval.equals(""))
					fields+=tbKey+"="+tbval+",";
				if(!hbval.equals(""))
					fields+=hbKey+"="+hbval+",";
				if(!ljtbval.equals(""))
					fields+=ljtbKey+"="+ljtbval+",";
				if(!ljhbval.equals(""))
					fields+=ljhbKey+"="+ljhbval+",";
				if(!sqval.equals(""))
					fields+=sqKey+"="+sqval+",";
				if(!sqljval.equals(""))
					fields+=sqljKey+"="+sqljval+",";
				if(fields.length()>1){
                    fields=fields.substring(0,fields.length()-1);
                }
				if(!fields.equals("")){
					updatesql+=fields;
					updatesql+=" where 1=1 "+updateWhereStr;
					sqls.add(updatesql);
					//System.out.println("update sql:"+updatesql);
				}
				
			} catch (ServletException e) {
				// TODO Auto-generated catch block
				//e.printStackTrace();
				System.out.println("!!!!!!!!!!!!"+factTableName+"exec error...");
				continue;
			}
		}
        try {
            //执行语句
        	//System.out.println("\nExec table:"+factTableName+";sqls count:"+sqls.size());
            if(sqls.size()>0)
                dao.execCommand(ConstUtil.DBNAME, sqls);
            return true;
        } catch (ServletException ex) {
            Logger.getLogger(KeyCalc.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        }
	}
	
	
	public String [] GetTimes(String time,String factTableName){
		int length=time.length();
		String [] rst=new String[2];
		SimpleDateFormat sdf =null;
		int hbtick=0;
		switch(length){
			case 8:{
				sdf= new SimpleDateFormat("yyyyMMdd");
				hbtick=Calendar.DATE;
				break;
			}
			case 6:{
				sdf= new SimpleDateFormat("yyyyMM");
				hbtick=Calendar.MONTH;
				break;
			}
			case 4:{
				sdf= new SimpleDateFormat("yyyy");
				hbtick=Calendar.YEAR;
				break;
			}
		}
		try {
			Date curDate=sdf.parse(time);
			Calendar  hbcalendar  =  Calendar.getInstance();  
			Calendar  tbcalendar  =  Calendar.getInstance();
			hbcalendar.setTime(curDate);  
			tbcalendar.setTime(curDate); 
			
			hbcalendar.add(hbtick, -1);
			tbcalendar.add(Calendar.YEAR, -1);
            String hbtime=sdf.format(hbcalendar.getTime());
            String tbtime=sdf.format(tbcalendar.getTime());
            
            rst[0]=tbtime;
            rst[1]=hbtime;
            //System.out.println("tbtime:"+tbtime+"  hbtime:"+hbtime);

		} catch (Exception e) {
			System.out.println("表"+factTableName+"时间字段有问题");
		}
		return rst;
	}


}
